package sat.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.util.math.Vector3D;
import org.mt4jx.components.visibleComponents.widgets.toolbar.MTToolbarButton;

import processing.core.PImage;
import sat.component.ClassComponent.entityType;
import sat.frame.WorkPlaceScene;

public class ToolbarIcon extends MTToolbarButton {
	private ToolbarIcon dragTaget = null;
	private Vector3D pos = null;
	public ToolbarIcon(PImage texture,  final MTApplication mtApplication, final WorkPlaceScene workPlaceScene) {
		super(texture, mtApplication);

		this.setSizeXYGlobal(60, 60);

		// Action : tap, click ...
		// Note: drag and drop is a kind of gesture
		this.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				switch (event.getID()) {
				case TapEvent.TAPPED:
					System.out.println("click on button");
					break;
				}
			}
		});

		this.unregisterAllInputProcessors();
		this.registerInputProcessor(new DragProcessor(mtApplication));
		this.addGestureListener(DragProcessor.class,
				new IGestureEventListener() {
			public boolean processGestureEvent(MTGestureEvent evt) {
				DragEvent de = (DragEvent) evt;
				switch (de.getId()) {
				case MTGestureEvent.GESTURE_STARTED:
					dragTaget = (ToolbarIcon) de.getTarget(); // Moves the component
					pos = new Vector3D(dragTaget.getPosition(TransformSpace.RELATIVE_TO_PARENT));
					System.out.println("Drag gesture started");
					break;
				case MTGestureEvent.GESTURE_UPDATED:
					System.out.println("Drag gesture updated");
					break;
				case MTGestureEvent.GESTURE_ENDED:
					ClassComponent c;
					System.out.println("name = "+dragTaget.getName());

					if(!workPlaceScene.getToolbarLeft().isGeometryContainsPointLocal(workPlaceScene.getToolbarLeft().globalToLocal(dragTaget.getPosition(TransformSpace.GLOBAL)))
							&& !workPlaceScene.getToolbarRight().isGeometryContainsPointLocal(workPlaceScene.getToolbarRight().globalToLocal(dragTaget.getPosition(TransformSpace.GLOBAL)))){
						if(dragTaget.getName() == "class" ){
							c = new ClassComponent(mtApplication, entityType.CLASS);
							c.setPositionGlobal(new Vector3D(dragTaget.getPosition(TransformSpace.GLOBAL)));
							workPlaceScene.addClassToList(c);
							workPlaceScene.getCanvas().addChild(c);
						}
						else if (dragTaget.getName() == "people" ){
							UseCaseActorComponent p = new UseCaseActorComponent(mtApplication);
							p.setPositionGlobal(new Vector3D(dragTaget.getPosition(TransformSpace.GLOBAL)));
							workPlaceScene.addActorToList(p);
							workPlaceScene.getCanvas().addChild(p);
						}	
						else if (dragTaget.getName() == "association" ){
							UseCaseAssociationComponent o = new UseCaseAssociationComponent(mtApplication);
							o.setPositionGlobal(new Vector3D(dragTaget.getPosition(TransformSpace.GLOBAL)));
							workPlaceScene.addAssociationToList(o);
							workPlaceScene.getCanvas().addChild(o);
						}
						else if (dragTaget.getName() == "interface" ){
							c = new ClassComponent(mtApplication, entityType.INTERFACE);
							c.setPositionGlobal(new Vector3D(dragTaget.getPosition(TransformSpace.GLOBAL)));
							workPlaceScene.addClassToList(c);
							workPlaceScene.getCanvas().addChild(c);
						}
						// TODO add diagram
					}
					dragTaget.setPositionRelativeToParent(pos);
					break;
				default:
					break;
				}
				return false;
			}
		});
	}
}
